# _*_ coding:utf-8 _*_
# 开发人员：zhu
# 项目名称：owner
# 开发时间：2022/2/2818:35
# 开发工具：PyCharm
import sys
import time

from PyQt5 import QtWidgets

import newsse
from untitled import Ui_DATA_OWNER
from newsse import *
from chaintest1 import *
from client.contractnote import ContractNote
from client.bcosclient import BcosClient
import os
from eth_utils import to_checksum_address
from client.datatype_parser import DatatypeParser
from client.common.compiler import Compiler
from client.bcoserror import BcosException, BcosError
from client_config import client_config
import sys
import traceback
# 从文件加载abi定义
#
def random_str(slen=10):
    seed = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-"
    sa = []
    for i in range(slen):
      sa.append(random.choice(seed))
    return ''.join(sa)
# def uptochain():
#
#     args = ['sdk测试']
#     client.sendRawTransactionGetReceipt(to_address, contract_abi, "set", args)

#     if os.path.isfile(client_config.solc_path) or os.path.isfile(client_config.solcjs_path):
#         Compiler.compile_file("contracts/HelloWorld.sol")
#     abi_file = "contracts/HelloWorld.abi"
#     data_parser = DatatypeParser()
#     data_parser.load_abi_file(abi_file)
#     contract_abi = data_parser.contract_abi
#     to_address = '0xa7331fea58f6ad68038ca84fc7fc02da7b24a478'
#     client = BcosClient()
#     args = ['sdk测试']
#     client.sendRawTransactionGetReceipt(to_address, contract_abi, "set", args)
#     client.finish()
class MyPyQT_Form(QtWidgets.QWidget, Ui_DATA_OWNER):

    def __init__(self):
        super(MyPyQT_Form, self).__init__()
        self.setupUi(self)


    # 实现pushButton_click()函数，textEdit是我们放上去的文本框的id
    def pushButton_click(self):
        # self.textEdit.setText("你点击了按钮")

        # self.textBroswer.setText("nihao")
        self.textBrowser.setText('数据初始化成功！关键字如下：\n' + str(worddict))

    def pushclick2(self):
        self.textBrowser_2.setText('构建安全索引【T】成功：\n' + str(T))
        self.textBrowser_4.setText('构建安全索引【A】成功：\n' + str(A))
    def pushclick5(self):
        self.textBrowser_5.setText('区块详细信息为：'+str(blockinfo))

    # def pushclick3(selt):
    def pushclick4(self):
        # self.uptochain()
        # aa=cqy()
        # if os.path.isfile(client_config.solc_path) or os.path.isfile(client_config.solcjs_path):
        #     Compiler.compile_file("contracts/HelloWorld.sol")
        # abi_file = "contracts/HelloWorld.abi"
        # data_parser = DatatypeParser()
        # data_parser.load_abi_file(abi_file)
        # contract_abi = data_parser.contract_abi
        # to_address = '0xa7331fea58f6ad68038ca84fc7fc02da7b24a478'
        # client = BcosClient()

        # time.sleep(3)
        self.textBrowser_3.setText('安全索引上链成功!\n安全索引哈希为：' + str(hashA)+'\n本次交易哈希为：'+str(txhash)+'\n当前块高为：'+str(resnumber))
        # client.finish()
        # print(repc)

if __name__ == '__main__':
    # print(str(newsse.intialization()))
    # newsse.main()
    # app=0
    abi_file = "contracts/HelloWorld.abi"
    data_parser = DatatypeParser()
    data_parser.load_abi_file(abi_file)
    contract_abi = data_parser.contract_abi
    to_address = '0xa7331fea58f6ad68038ca84fc7fc02da7b24a478'
    client = BcosClient()
    args = [random_str(10)]
    receipt=client.sendRawTransactionGetReceipt(to_address, contract_abi, "set", args)
    res = client.call(to_address, contract_abi, "get")
    txhash = receipt['transactionHash']
    # uptochain()
    resnumber = client.getBlockNumber()
    blockinfo=client.getBlockByNumber(resnumber)
    T, A, worddict, hashA = newsse.main()
    app = QtWidgets.QApplication(sys.argv)
    my_pyqt_form = MyPyQT_Form()
    my_pyqt_form.show()
    # uptochain()
    client.finish()
    # sys.exit(0)
    sys.exit(app.exec_())
